package com.googlecode.allinclusive.fw.coderesource;

import java.util.List;
import java.util.Map;

public abstract class AbstractCodeManager implements CodeManager {

	/* Cache */
	protected Map<String, List<CodeValue>> cacheMap;
	
	public List<CodeValue> getCodeValueList(String codeId) {
		assert (codeId != null);
		if (cacheMap == null) {
			load();
		}
		
		List<CodeValue> codeList = cacheMap.get(codeId);
		if (codeList == null || codeList.isEmpty()) {
			throw new RuntimeException("no resource found.[id=" + codeId + "]");
		}
		
		return codeList;
	}

	public CodeValue getCodeValue(String codeId, String codeNo) {
		assert (codeId != null);
		assert (codeNo != null);
		
		List<CodeValue> codeList = getCodeValueList(codeId);
		
		for (CodeValue codeValue : codeList) {
			if (codeNo.equals(codeValue.getCodeNo())) {
				return codeValue;
			}
		}
		
		throw new RuntimeException("no resource found.[id=" + codeId + ", no=" + codeNo + "]");
	}
	
	public abstract void load();
}
